/**
 * @copyright SilverBiology, LLC
 * @author SilverBiology, LLC
 * @website http://www.silverbiology.com
*/
Ext.ns("BioBlitz");

BioBlitz.MasterMapPanel = function(config){
	
	var position = new google.maps.LatLng(30, -90);
	
	var mapOptions = {
			center: position
		,	zoom: 6
		,	mapTypeId: google.maps.MapTypeId.TERRAIN
	}
	
	var map = new Ext.Map({
			mapOptions: mapOptions
		,	id: 'masteridmap'	
	});			  
				  
	Ext.apply(this, config, { 
			items: [
				map
			]
		,	iconCls: 'search'
		,	dockedItems: [{
					xtype: 'toolbar'
				,	dock: 'top'
				,	title: 'Master Map'
				,	items: [{
						xtype: 'spacer'
					}, {
							text: 'Refresh'
						,	ui: 'action'
						,	id: 'mmrefresh'
						,	iconCls: 'refresh'
						,	handler: function() {
								var zoomLevel = Ext.getCmp('masteridmap').map.getZoom();
								Ext.getCmp('masteridmap').map.setZoom(zoomLevel * 1);						
							}	
					}]
			}]
		,	listeners: {
				activate: this.addLayermasterid
			}
	});
		
	BioBlitz.MasterMapPanel.superclass.constructor.call(this,config);
	
};

Ext.extend(BioBlitz.MasterMapPanel , Ext.Panel, {
		
		addLayermasterid : function() {
			var master_id = localStorage.getItem('masterid');
			if (!(master_id == "")) {
				if(!Ext.isEmpty(this.layer )){
//					console.log("this.layer",this.layer);
					if(this.layer .tableId == master_id)
							return;  			
				}
				this.layer = new google.maps.FusionTablesLayer(master_id);
				this.layer.setMap(Ext.getCmp('masteridmap').map);
				var zoomLevel = Ext.getCmp('masteridmap').map.getZoom();
				Ext.getCmp('masteridmap').map.setZoom(zoomLevel * 1);
			} else {
				if (!(this.layer)) {
					alert(" No Master ID Available! ");
				} else {
					this.layer.setMap(null);
					alert(" No Master ID Available! ");
				}	
			}
		}

});